package fr.bordeaux1.planingdesetudiants.view;

import java.sql.SQLException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Text;

import fr.bordeaux1.planingdesetudiants.controler.CreneauFormater;
import fr.bordeaux1.planingdesetudiants.model.Enseignant;
import org.eclipse.swt.layout.GridData;

public class CompositeEns extends Composite implements IOnglet {
	private IFenetrePrincipale fenetrePrincipale;
	private Text textName;
	private Text textSurname;
	private List listEns;
	private DisponibiliteToggleButton[] choix;
	private Group grpAjouterUnEnseignant;
	private Button delete;
	private Button save;
	private java.util.List<Enseignant> enseignants;

	public CompositeEns(Composite arg0, int arg1,
			IFenetrePrincipale fenetrePrincipale) {
		super(arg0, arg1);
		this.fenetrePrincipale = fenetrePrincipale;
		setLayout(new FormLayout());

		listEns = new List(this, SWT.V_SCROLL | SWT.BORDER);
		FormData fd_listeEns = new FormData();
		fd_listeEns.top = new FormAttachment(0, 5);
		fd_listeEns.right = new FormAttachment(0, 158);
		fd_listeEns.left = new FormAttachment(0, 5);
		listEns.setLayoutData(fd_listeEns);
		listEns.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				afficherDispo();
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				afficherDispo();

			}
		});

		grpAjouterUnEnseignant = new Group(this, SWT.NONE);
		grpAjouterUnEnseignant.setLayout(new FormLayout());
		FormData fd_grpAjouterUntudiant = new FormData();
		fd_grpAjouterUntudiant.left = new FormAttachment(listEns, 6);
		fd_grpAjouterUntudiant.top = new FormAttachment(0, 6);
		fd_grpAjouterUntudiant.right = new FormAttachment(100, -10);
		grpAjouterUnEnseignant.setLayoutData(fd_grpAjouterUntudiant);
		grpAjouterUnEnseignant.setText("Ajouter un enseignant");

		Label nameE = new Label(grpAjouterUnEnseignant, SWT.NONE);
		nameE.setText("Nom :");
		FormData fd_nameE = new FormData();
		fd_nameE.top = new FormAttachment(0, 8);
		fd_nameE.left = new FormAttachment(0, 5);
		nameE.setLayoutData(fd_nameE);
		textName = new Text(grpAjouterUnEnseignant, SWT.BORDER);
		FormData fd_textName = new FormData();
		fd_textName.top = new FormAttachment(nameE, -3, SWT.TOP);
		fd_textName.left = new FormAttachment(nameE, 6);
		fd_textName.right = new FormAttachment(100, -5);
		textName.setLayoutData(fd_textName);

		Label surnameE = new Label(grpAjouterUnEnseignant, SWT.NONE);
		surnameE.setText("Prenom :");
		FormData fd_surnameE = new FormData();
		fd_surnameE.top = new FormAttachment(0, 34);
		fd_surnameE.left = new FormAttachment(0, 5);
		surnameE.setLayoutData(fd_surnameE);
		textSurname = new Text(grpAjouterUnEnseignant, SWT.BORDER);
		FormData fd_textSurname = new FormData();
		fd_textSurname.top = new FormAttachment(surnameE, -3, SWT.TOP);
		fd_textSurname.left = new FormAttachment(surnameE, 6);
		fd_textSurname.right = new FormAttachment(100, -5);
		textSurname.setLayoutData(fd_textSurname);

		Button addTeach = new Button(grpAjouterUnEnseignant, SWT.NONE);
		addTeach.setText("Ajouter");
		FormData fd_addTeach = new FormData();
		fd_addTeach.bottom = new FormAttachment(100, -3);
		fd_addTeach.top = new FormAttachment(textSurname, 3, SWT.DEFAULT);
		fd_addTeach.right = new FormAttachment(50, 50);
		fd_addTeach.left = new FormAttachment(50, -50);
		addTeach.setLayoutData(fd_addTeach);
		addTeach.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				ajouterEnseignant();
				CompositeEns.this.fenetrePrincipale.updateStatusBar("Nouvel enseignant ajouté avec succès");
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				ajouterEnseignant();

			}
		});

		Composite table = new Composite(this, SWT.BORDER);
		FormData fd_table = new FormData();
		fd_table.top = new FormAttachment(grpAjouterUnEnseignant, 10,
				SWT.DEFAULT);
		fd_table.left = new FormAttachment(listEns, 6);
		fd_table.right = new FormAttachment(100, -5);
		table.setLayoutData(fd_table);

		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 6;
		gridLayout.makeColumnsEqualWidth = true;
		table.setLayout(gridLayout);
		Label horaire = new Label(table, 0);
		horaire.setText("Horaire");
		GridData gd_horaire = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_horaire.minimumHeight = 20;
		gd_horaire.minimumWidth = 70;
		horaire.setLayoutData(gd_horaire);

		delete = new Button(this, SWT.NONE);
		delete.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				if (listEns.getSelectionIndex() >= 0) {
					Enseignant ens = enseignants.get(listEns
							.getSelectionIndex());
					try {
						CompositeEns.this.fenetrePrincipale.getDb()
								.supprimerEnseignant(ens);
						refreshData();
						CompositeEns.this.fenetrePrincipale.updateStatusBar("Enseignant supprimé avec succès");
					} catch (SQLException e) {
						e.printStackTrace();
					}
					refreshData();
				}
			}
		});
		delete.setEnabled(false);
		fd_listeEns.bottom = new FormAttachment(delete, -6);
		FormData fd_delete = new FormData();
		fd_delete.right = new FormAttachment(listEns, 0, SWT.RIGHT);
		fd_delete.bottom = new FormAttachment(100, -5);
		fd_delete.left = new FormAttachment(0, 5);
		delete.setLayoutData(fd_delete);
		delete.setText("Supprimer");

		save = new Button(this, SWT.NONE);
		fd_table.bottom = new FormAttachment(save, -6);
		FormData fd_save = new FormData();
		// fd_save.top = new FormAttachment(table, 0, SWT.RIGHT);
		fd_save.right = new FormAttachment(50, 50);
		fd_save.left = new FormAttachment(50, -50);
		fd_save.bottom = new FormAttachment(100, -5);
		save.setLayoutData(fd_save);
		save.setText("Sauvegarder");
		save.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				saveDispo();
				CompositeEns.this.fenetrePrincipale.updateStatusBar("Enseignant mis à jour avec succès");
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				saveDispo();
			}
		});
		gridLayout.numColumns = 6;
		gridLayout.makeColumnsEqualWidth = true;

		table.setLayout(gridLayout);

		for (int trancheHoraire = 0; trancheHoraire < 4; trancheHoraire++) {
			if (trancheHoraire == 2) {
				CLabel ll = new CLabel(table, SWT.NONE);
				ll.setText("Pause");
				ll.setAlignment(SWT.CENTER);

				ll.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false,
						1, 6));
			}
			Label heure = new Label(table, SWT.NONE);
			heure.setAlignment(SWT.CENTER);
			heure.setText(CreneauFormater.formatHeure(trancheHoraire + 1));
			GridData gd_heure = new GridData(SWT.FILL, SWT.TOP, false, false,
					1, 1);

			gd_heure.minimumHeight = 20;
			// gd_heure.minimumWidth = 70;
			heure.setLayoutData(gd_heure);
		}

		choix = new DisponibiliteToggleButton[20];
		for (int jour = 0; jour < 5; jour++) {

			Label l = new Label(table, 0);
			l.setText(CreneauFormater.getJour(jour));

			for (int trancheHoraire = 0; trancheHoraire < 4; trancheHoraire++) {

				final int creneau = 4 * jour + trancheHoraire;
				DisponibiliteToggleButton dtb = new DisponibiliteToggleButton(
						table, creneau);
				dtb.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseUp(MouseEvent arg0) {

						((DisponibiliteToggleButton) arg0.getSource()).toggle();
					}
				});

				choix[creneau] = dtb;

				GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);

				gd.minimumWidth = 100;
				gd.minimumHeight = 30;
				dtb.setLayoutData(gd);

			}
		}
	}

	@Override
	public void refreshData() {
		listEns.removeAll();
		textName.clearSelection();
		textSurname.clearSelection();
		delete.setEnabled(false);
		textName.setText("");
		textSurname.setText("");
		for (DisponibiliteToggleButton tb : choix) {
			tb.setIndisponible(false);
		}
		try {
			enseignants = fenetrePrincipale.getDb().listerEnseignant();
			for (Enseignant enseignant : enseignants) {
				listEns.add(enseignant.getNom() + " " + enseignant.getPrenom());
				listEns.setData(
						enseignant.getNom() + " " + enseignant.getPrenom(),
						enseignant);
			}
		} catch (SQLException e) {
			// TODO popup erreur et kill
			e.printStackTrace();
		}

	}

	public void ajouterEnseignant() {
		if (textName.getText().length() > 0
				|| textSurname.getText().length() > 0) {
			Enseignant enseignant = new Enseignant();
			enseignant.setNom(textName.getText());
			enseignant.setPrenom(textSurname.getText());
			for (int i = 0; i < 20; i++)
				enseignant.setDisponible(i + 1, !choix[i].isIndisponible());

			try {
				fenetrePrincipale.getDb().ajouterEnseignant(enseignant);
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				refreshData();
			}
		} else {
			//TODO afficher message erreur
		}
	}

	public void afficherDispo() {
		if (listEns.getSelectionIndex() >= 0) {
			Enseignant enseignant = enseignants
					.get(listEns.getSelectionIndex());

			for (int i = 0; i < 20; i++) {
				choix[i].setIndisponible(!enseignant.isDisponible(i + 1));
			}
			delete.setEnabled(true);
		}
	}

	public void saveDispo() {
		if (listEns.getSelectionIndex() >= 0) {
			Enseignant ens = enseignants.get(listEns.getSelectionIndex());
			try {
				for (int i = 0; i < 20; i++) {
					ens.setDisponible(i + 1, !choix[i].isIndisponible());
				}
				fenetrePrincipale.getDb().modifierEnseignant(ens);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			fenetrePrincipale.modificationApportees();
		}
	}
}
